package com.zy.web.demo.wenjian;

import java.io.*;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.concurrent.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

/**
 * @author zy
 * @version 1.0
 * @date 2021/4/13 20:46
 */
public class DemoUpgrade1 {

    private static final long awaitTime = 4*60;

    private static final String filePath = "E:\\zy\\超星项目\\镜像小组\\GroupTopic_WEB-Image\\src\\main\\webapp";

    private static final String DepositPath = "C:\\Users\\pc\\Desktop\\GroupTopic_WEB-Image.txt";

    private static List<String> list = new ArrayList<String>();

    private static List<String> matchRegexList = new LinkedList<>();

    private static ExecutorService executorService = Executors.newFixedThreadPool(5);

    private static ThreadPoolExecutor threadPool = new ThreadPoolExecutor(10,
            100,
            100L,
            TimeUnit.SECONDS,
            new ArrayBlockingQueue<Runnable>(50),
            Executors.defaultThreadFactory(),
            new ThreadPoolExecutor.AbortPolicy()
            );

    public static void main(String[] args) throws InterruptedException {
        long StartTime = System.currentTimeMillis();
        getFiles(filePath);
        try {
            executorService.shutdown();
            if ( !executorService.awaitTermination(awaitTime,TimeUnit.SECONDS) ){
                executorService.shutdownNow();
            }
        }catch (InterruptedException e){
            System.out.println("线程池强制结束："+e);
            executorService.shutdownNow();
        }
        while (true){
            if (executorService.isTerminated()){
                Duplicate();
                long EndTime = System.currentTimeMillis();
                System.out.println("耗时："+(EndTime-StartTime));
                break;
            }
        }
    }

    public static void getFiles(String filePath)  {
        // 遍历所有文件
        File file = new File(filePath);
        File[] files = file.listFiles();
        for (File file1 : files) {
            if ( file1.isDirectory() ){
                getFiles(file1.getAbsolutePath());
            }else {
                // 读取文件
                // 升级线程
                executorService.execute(new Runnable() {
                    @Override
                    public void run() {
                        FileInput(matchRegexList, file1);
                    }
                });
            }
        }
    }

    static void FileInput(List<String> matchRegexList, File file) {
        try {
            BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(file)));
            String http = "";
            String str = null;
            while((str = reader.readLine()) != null){
                http +=str;
            }
            //正则表达式 匹配http
            String regex = "(https?|ftp|file)://[-A-Za-z0-9+&@#/%?=~_|!:,.;]+[-A-Za-z0-9+&@#/%=~_|]";
            Pattern pattern = Pattern.compile(regex);
            Matcher m = pattern.matcher(http);
            while(m.find()){
                // 存在则把http提取出来存入文本中
//                String group = m.group();
//                String[] split = group.split("/");
                matchRegexList.add(m.group()+"~~"+file.getAbsolutePath());
            }
            // 关闭流
            reader.close();
            System.out.println(matchRegexList.size());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void FileInput(File file,List<String> matchRegexList){

        FileInput(matchRegexList, file);
    }

    private static void Duplicate(){
        FileOutputStream os = null;
        try {
            os = new FileOutputStream(new File(DepositPath),true);
            System.out.println("总数："+matchRegexList.size());
            for (int i = 0; i < matchRegexList.size(); i++) {
                os.write(matchRegexList.get(i).getBytes());
                os.write("\r\n".getBytes());
                System.out.println("~~~~~~~~"+i+" -"+matchRegexList.get(i));
            }
            os.flush();
            os.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }



}
